local snow    = require("meet_your_mods.mhr").snow
local status  = require("meet_your_mods.status")
local setting = require("meet_your_mods.setting")
local ui      = require("meet_your_mods.ui")
local input   = require("meet_your_mods.input")

snow.data.DataDef.PlKitchenSkillId:fetch_enum("moxie", "Pl_KitchenSkill_024") --团子毅力
snow.data.DataDef.PlEquipSkillId:fetch_enum("guts", "Pl_EquipSkill_220")      --装备毅力
snow.data.ContentsIdSystem.ItemId:fetch_enum("aurortle", "I_EC_0004")         --涂龟

local escape_master = {
    setting = {
        abandon_before_wasted = false, --去世前自动重置
        abandon_before_failed = false, --任务失败前自动重置
        abandon_by_pad = false,        --通过手柄重置
        abandon_by_key = false,        --通过键盘重置
        abandon_pad = snow.Pad.Button.NONE,
        abandon_key = 0,

        skip_by_pad = false, --通过手柄跳过返回倒计时
        skip_by_key = false, --通过键盘跳过返回倒计时
        skip_pad = snow.Pad.Button.NONE,
        skip_key = 0,

        remove_camera = true, --移除任务结束时的特写镜头
        fast_return   = true, --快速结束返回动画
    },
    min_health  = 64,
    guts_health = {
        [1] = 80,
        [2] = 72,
        [3] = 64,
    },
}

--去世前自动重置任务
function escape_master:pre_calcVital(parameters)
    if not self.setting.abandon_before_wasted
        or status:current_status() ~= status.logical_status.quest
        or status:is_online()
    then
        return sdk.PreHookResult.CALL_ORIGINAL
    end

    local player      = snow.player.PlayerBase(sdk.to_managed_object(parameters[2]))
    local player_data = player:get_PlayerData()

    local health = player_data:get_vital()
    if health + sdk.to_float(parameters[3]) <= 0 then
        local player_skills = player:get_PlayerSkillList()
        local guts          = player_skills:getSkillData(snow.data.DataDef.PlEquipSkillId.guts)
        local pouch         = snow.data.DataManager:get_singleton():getEcItemPouch()

        --排除团子毅力技能/装备毅力技能/涂龟有效的情况
        if health < self.min_health
            or not player_skills:hasKitchenSkill(snow.data.DataDef.PlKitchenSkillId.moxie)
                and 0 == pouch:getItemCount(snow.data.ContentsIdSystem.ItemId.aurortle)
                and (not guts or health < self.guts_health[guts.SkillLv])
        then
            snow.QuestManager:get_singleton():notifyReset()
            return sdk.PreHookResult.SKIP_ORIGINAL
        end
    end

    return sdk.PreHookResult.CALL_ORIGINAL
end

--失败前自动重置任务
function escape_master:pre_setQuestFail(_)
    if self.setting.abandon_before_failed
        and not status:is_online()
    then
        snow.QuestManager:get_singleton():notifyReset()
        return sdk.PreHookResult.SKIP_ORIGINAL
    end

    return sdk.PreHookResult.CALL_ORIGINAL
end

--快速重置任务
function escape_master:pre_updateQuestTime(_)
    if not self.setting.abandon_by_pad and not self.setting.abandon_by_key
        or status:is_online()
    then
        return sdk.PreHookResult.CALL_ORIGINAL
    end

    if self.setting.abandon_by_pad and self.setting.abandon_pad ~= snow.Pad.Button.NONE
        and input:pad_andOn(self.setting.abandon_pad)
        or self.setting.abandon_by_key and self.setting.abandon_key ~= 0
        and input:key_getTrg(self.setting.abandon_key)
    then
        snow.QuestManager:get_singleton():notifyReset()
    end

    return sdk.PreHookResult.CALL_ORIGINAL
end

--快速结束返回动画/快速结束返回倒计时
function escape_master:pre_updateQuestEndFlow(_)
    if not self.setting.fast_return
        and not self.setting.skip_by_pad
        and not self.setting.skip_by_key
        or snow.QuestManager.Status.Failed == snow.QuestManager:get_singleton():getStatus()
    then
        return sdk.PreHookResult.CALL_ORIGINAL
    end

    local end_flow = status:quest_ending_flow()
    if snow.QuestManager.EndFlow.WaitEndTimer == end_flow
        and (self.setting.skip_by_pad and self.setting.skip_pad ~= snow.Pad.Button.NONE
            and input:pad_andOn(self.setting.skip_pad)
            or self.setting.skip_by_key and self.setting.skip_key ~= 0
            and input:key_getTrg(self.setting.skip_key))
        or snow.QuestManager.EndFlow.CameraDemo == end_flow
            and self.setting.fast_return
    then
        snow.QuestManager:get_singleton()._QuestEndFlowTimer = 0
    end

    return sdk.PreHookResult.CALL_ORIGINAL
end

--移除任务结束时的特写镜头
function escape_master:pre_RequestActive(prameter)
    if self.setting.remove_camera
        and snow.CameraManager.CameraType.DemoCamera == sdk.to_int64(prameter[3])
        and snow.QuestManager.EndFlow.Start == status:quest_ending_flow()
    then
        return sdk.PreHookResult.SKIP_ORIGINAL
    end

    return sdk.PreHookResult.CALL_ORIGINAL
end

function escape_master.initialize(self)
    snow.player.PlayerQuestBase.calcVital:sdk_auto_hook(self)
    snow.QuestManager.setQuestFail:sdk_auto_hook(self)
    snow.QuestManager.updateQuestTime:sdk_auto_hook(self)
    snow.QuestManager.updateQuestEndFlow:sdk_auto_hook(self)
    snow.CameraManager.RequestActive:sdk_auto_hook(self)

    setting:bind_setting("escape_master", self.setting)

    ui:cluster("fatal_quest", "任务关键功能")
    ui:spacing("fatal_quest")
    ui:static_text("fatal_quest", "自动重置任务", nil)
    ui:checkbox("fatal_quest", "escape_master", "abandon_before_wasted", "在去世前", nil, true)
    ui:checkbox("fatal_quest", "escape_master", "abandon_before_failed", "在失败前", nil, true)

    ui:spacing("fatal_quest")
    ui:static_text("fatal_quest", "快速重置", nil)
    ui:checkbox("fatal_quest", "escape_master", "abandon_by_pad", "通过手柄:", nil, true)
    ui:pad_button("fatal_quest", "escape_master", "abandon_pad")
    ui:checkbox("fatal_quest", "escape_master", "abandon_by_key", "通过键盘:", nil, true)
    ui:key_button("fatal_quest", "escape_master", "abandon_key")

    ui:cluster("easy_quest", "任务便利功能")
    ui:checkbox("easy_quest", "escape_master", "remove_camera", "移除任务结束特写")
    ui:checkbox("easy_quest", "escape_master", "fast_return",   "快速结束返回动画")

    ui:spacing("easy_quest")
    ui:static_text("easy_quest", "快速返回", nil)
    ui:checkbox("easy_quest", "escape_master", "skip_by_pad", "通过手柄:", nil, true)
    ui:pad_button("easy_quest", "escape_master", "skip_pad")
    ui:checkbox("easy_quest", "escape_master", "skip_by_key", "通过键盘:", nil, true)
    ui:key_button("easy_quest", "escape_master", "skip_key")
end

function escape_master:normalize()
    self.setting.abandon_pad = math.floor(self.setting.abandon_pad)
    self.setting.abandon_key = math.floor(self.setting.abandon_key)
    self.setting.skip_pad    = math.floor(self.setting.skip_pad)
    self.setting.skip_key    = math.floor(self.setting.skip_key)
end

return escape_master